package com.example.donghua;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements Animation.AnimationListener{

    private AnimationSet set;
    private AlphaAnimation alphaAnimation;
    private Animation animation;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = findViewById(R.id.tv);

        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setFillAfter(true);

        tv.startAnimation(scaleAnimation);

        //动画监听 开始 重复 结束
        scaleAnimation.setAnimationListener(this);

        initAnimation();
        initXMLAnimation();
        initAnimationSet();
    }

    // 加载 xml 动画
    private void initXMLAnimation() {
        // 使用 动画工具类  加载xml动画  成动画对象
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.myalpha);
        animation.setFillAfter(true);

        // 执行动画
        tv.startAnimation(animation);
    }
    //  java   代码完成 动画效果
    private void initAnimation() {
        //透明动画
         animation = new AlphaAnimation(0, 1);

    }

    private void initAnimationSet() {
        //集合动画
        set = new AnimationSet(this, null);
        //透明动画
        alphaAnimation = new AlphaAnimation(0, 1);
        set.addAnimation(alphaAnimation);
        //平移
        animation = new TranslateAnimation(0, 100, 0, 0);
        set.addAnimation(animation);
        //旋转
        animation = new RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //缩放
        animation = new ScaleAnimation(1, 2, 1, 1
                , Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        set.addAnimation(animation);
        //基本设置  设置动画的执行过程时长
        //           设置是否保持执行后的状态
        set.setDuration(3000);
        set.setFillAfter(true);
    }
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
       startActivity(new Intent(this,MainActivity2.class));
       overridePendingTransition(R.anim.enter,R.anim.exit);//打开 关闭 进场动画
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}